﻿///<reference path="../../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../../scripts/progressbar/jquery.progressbar.js" />
///<reference path="../../../scripts/swfupload/swfupload.js" />
///<reference path="../../../scripts/swfupload/jquery.swfupload.js" />
///<reference path="../../../scripts/jquery.prettyPhoto.js" />

$(function () {
    jQuery.fn.bindAll = function (options) {
        var $this = this;
        jQuery.each(options, function (key, val) {
            $this.bind(key, val);
        });
        return this;
    }
    $(function () {
        $('#photo-upload-control').swfupload({
            upload_url: upload_url,
            file_size_limit: "2 MB",
            file_types: "*.jpg; *.jpeg; *.png; *.gif; *.GIF; *.PNG;",
            file_types_description: "Images",
            file_upload_limit: "0",
            button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,

            flash_url: flash_url,
            button_image_url: button_image_url,
            button_width: 160,
            button_height: 22,
            button_placeholder: $('.photo-button', this)[0],
            button_text: '<span class="button">' + label + ' <span class="buttonSmall">(2 MB Max)</span></span>',
            button_text_style: ".button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }",
            button_text_top_padding: 1,
            button_text_left_padding: 5,
            button_action: SWFUpload.BUTTON_ACTION.SELECT_FILE,
            post_params: { "Name": $('#Name').val() },
            debug: false
        });
    });



    $(function () {

        var listeners = {
            swfuploadLoaded: function (event) {
            },
            fileQueued: function (event, file) {
            },
            fileQueueError: function (event, file, errorCode, message) {
            },
            fileDialogStart: function (event) {
            },
            fileDialogComplete: function (event, numFilesSelected, numFilesQueued) {
                $(this).swfupload('setPostParams', { "Name": $('#Name').val(), "OldName": $('#Photo').val() });
                $(this).swfupload('startUpload');
            },
            uploadStart: function (event, file) {
                $("#photo-upload-control .progressbar").show();
                $("#photo-upload-control .progressbar").reportprogress(0);
            },
            uploadProgress: function (event, file, bytesLoaded) {
                $("#photo-upload-control .progressbar").reportprogress(bytesLoaded, file.size);
            },
            uploadSuccess: function (event, file, serverData) {
                var data = eval('(' + serverData + ')');

                $('.log', this).empty();
                $('.thumbnails a', this).attr('href', data.ThumbnailName);
                $('.thumbnails img', this).attr('src', data.ThumbnailName);

                //var paras = serverData.split("/");
                var paras = data.ThumbnailName.split('/');
                var fileName = paras[paras.length - 1];

                $("#Photo").val(fileName);

                //Gallery
                $("a[rel^='prettyPhoto']").prettyPhoto();
            },
            uploadComplete: function (event, file) {
                $("#photo-upload-control .progressbar").hide();
            },
            uploadError: function (event, file, errorCode, message) {
                $('.log', this).text('upload error - ' + message);
            }
        };

        $('#photo-upload-control').bindAll(listeners);
    });

    //Gallery
    $("a[rel^='prettyPhoto']").prettyPhoto();
});